package com.youtills.core.lang;

/**
 * Created by IntelliJ IDEA.
 * User: ravi
 * Date: 9/20/12
 * Time: 11:27 AM
 * To change this template use File | Settings | File Templates.
 */
public class ObjectUtil {

    private ObjectUtil() {
    }

    public static <T> T firstNonNull(T... objects){
        if(objects != null && objects.length > 0){
            for(T t : objects){
                if(t != null){
                    return  t;
                }
            }
        }
        return null;
    }

    public static <T> boolean areNotEquals(T o1, T o2) {
        return !areEquals(o1, o2);
    }
    public static <T> boolean areEquals(T o1, T o2) {
        //If both null then o1 == o2 should be true.
        return o1 == o2 || (o1 != null && o1.equals(o2));

    }

    public  static <T> boolean areEquals (T... objects) {
        if(objects == null || objects.length == 1) {
            return true;
        }

        T first = objects[0];
        for(int i = 1; i < objects.length; i++){
            if (areNotEquals(first, objects[i])){
                return false;
            }
        }

        return true;

    }
}
